package com.yh.csx.crm.webapi.controller;

import com.yh.csx.crm.appapi.controller.BaseController;
import com.yh.csx.crm.core.entity.CommonResponse;
import com.yh.csx.crm.core.enums.ApprovalTypeEnum;
import com.yh.csx.crm.core.enums.CreditLevelDimensionEnum;
import com.yh.csx.crm.core.enums.MessageEnum;
import com.yh.csx.crm.core.util.DateUtils;
import com.yh.csx.crm.service.MessageService;
import com.yh.csx.crm.service.request.BankNumberReq;
import com.yh.csx.crm.service.request.CreditLevelReq;
import com.yh.csx.crm.service.request.UploadFile;
import com.yh.csx.crm.core.util.BeanUtils;
import com.yh.csx.crm.po.SysSalesPermission;
import com.yh.csx.crm.service.CacheService;
import com.yh.csx.crm.service.PublicService;
import com.yh.csx.crm.service.response.*;
import com.yh.csx.crm.service.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * PublicController class
 *
 * @author huyalin
 * @date 2019/2/22
 */

@Api("公共服务")
@RestController
@RequestMapping("/public")
public class PublicController extends BaseController {

    @Autowired
    PublicService publicService;

    @Autowired
    CacheService cacheService;


    @ApiOperation("获取银行联行号")
    @GetMapping("/bank_number")
    public CommonResponse<PageWrap<BankNumberResp>> getBankNumber(BankNumberReq req) {
        return super.visit(() -> {
            return publicService.getBankNumber(req);
        });
    }

    @ApiOperation("获取客户名称")
    @GetMapping("/customer_name")
    public CommonResponse<List<String>> getCustomerName(String customerName) {
        return super.visit(() -> {
            return publicService.getCustomerNames(customerName);
        });
    }

    @ApiOperation("获取销售业务员")
    @GetMapping("/sales_user")
    public CommonResponse<List<SalesUserResp>> getSalesUser(String phoneOrName) {
        return super.visit(() -> {
            List<SysSalesPermission> salesUser = publicService.getSalesUser(phoneOrName);
            return BeanUtils.convertList(SalesUserResp.class, salesUser);
        });
    }

    @ApiOperation("获取客户分类")
    @GetMapping("/customer_category")
    public CommonResponse<List<CategoryTree>> getCustomerCategory() {
        return super.visit(() -> {
            return publicService.getCustomerCategory();
        });
    }

    @ApiOperation("获取地区信息")
    @GetMapping("/region")
    public CommonResponse<List<RegionTree>> getRegion() {
        return super.visit(() -> {
            return publicService.getRegion();
        });
    }

    @ApiOperation("获取销售省区")
    @GetMapping("/province")
    public CommonResponse<List<SalesProvinceAreaResp>> getSysProvince() {
        return super.visit(() -> {
            return publicService.getSysProvince();
        });
    }

    @ApiOperation("获取公司代码")
    @GetMapping("/company_code")
    public CommonResponse<List<CompanyCodeResp>> getCompanyCode() {
        return super.visit(() -> {
            return publicService.getCompanyCode();
        });
    }

    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public CommonResponse<UploadFile> uploadFile(MultipartFile file) {
        return visit(() -> {
            return publicService.uploadFiles(file);
        });
    }

    @ApiOperation("获取门店列表")
    @GetMapping("/shop")
    public CommonResponse<List<ShopLocationResp>> getShopLocationList(String keyword, Integer pageSize) {
        return visit(() -> {
            List<ShopLocationResp> list = cacheService.getValue("publicService::getShopLocationList", 3600000, publicService::getShopLocationList);
            if (list == null) return Collections.emptyList();
            return list.stream().filter(resp -> StringUtils.isBlank(keyword) || (resp.getLocationCode().contains(keyword) || resp.getOrgShortName().contains(keyword)))
                                .limit(pageSize == null || pageSize <= 0 ? list.size() : pageSize)
                                .collect(Collectors.toList());
        });
    }

    @ApiOperation("获取档案分类")
    @GetMapping("/archive_category")
    public CommonResponse<List<CreditLevelItemResp>> getArchiveCategory() {
        return visit(() -> {
            return publicService.getCreditLevelItem(CreditLevelDimensionEnum.ARCHIVE_CATEGORY);
        });
    }

    @ApiOperation("获取客户性质")
    @GetMapping("/customer_nature")
    public CommonResponse<List<CreditLevelItemResp>> getCustomerNature() {
        return visit(() -> {
            return publicService.getCreditLevelItem(CreditLevelDimensionEnum.CUSTOMER_NATURE);
        });
    }

    @ApiOperation("注册资本&经营年限")
    @GetMapping("/registered_capital")
    public CommonResponse<List<CreditLevelItemResp>> getRegisteredCapital() {
        return visit(() -> {
            return publicService.getCreditLevelItem(CreditLevelDimensionEnum.REGISTERED_CAPITAL);
        });
    }

    @ApiOperation("客户级别计算")
    @GetMapping("/credit_level")
    public CommonResponse<CreditLevelResultResp> getCreditLevelResult(@Validated CreditLevelReq req) {
        return super.visit(() -> {
            return publicService.getCreditLevelResult(req);
        });
    }

    @Autowired
    private MessageService messageService;
    @ApiOperation("发送短信通知")
    @GetMapping("/send_message")
    public CommonResponse sendMessage(String phone, String type) {
        return super.visit(() -> {
            String code = null;
            if (type.equals("01")) {
                messageService.sendMessage(MessageEnum.START_PERSON.getCode(), Arrays.asList(phone), DateUtils.formatDateTime(new Date()),
                        "12345678", ApprovalTypeEnum.NEW.getName(), "通过");
            }
            if (type.equals("02")) {
                messageService.sendMessage(MessageEnum.TODO_PERSON.getCode(), Arrays.asList(phone), DateUtils.formatDateTime(new Date()),
                        "23432423", ApprovalTypeEnum.CREDIT.getName());
            }

        });
    }

}
